package one.three.thirtyTwo;

public class Steque<Item> {

	private Node<Item> node;

	private static class Node<Item> {
		private Item item;
		private Node next;

		@Override
		public String toString() {
			return "[" + item + "]";
		}
	}

	public void push(Item item) {
		if (node == null) {
			node = new Node<Item>();
			node.item = item;
		} else {
			Node<Item> newNode = new Node<Item>();
			newNode.item = item;
			newNode.next = node;
			node = newNode;
		}
	}

	public void enqueue(Item item) {
		if (node == null) {
			node = new Node<Item>();
			node.item = item;
		} else {
			Node<Item> newNode = new Node<Item>();
			newNode.item = item;
			Node<Item> c = node;
			while (c.next != null) {
				c = c.next;
			}
			c.next = newNode;
		}
	}

	public Item pop() {
		if (node != null) {
			Item item = node.item;
			node = node.next;
			return item;
		} else {
			return null;
		}
	}

	public boolean isEmpty() {
		return node == null;
	}

	public static void main(String[] args) throws Exception {
		Steque<String> s = new Steque<String>();
		for (int i = 0; i < 5; i++) {
			s.push(i + "");
		}
		s.enqueue("x");
		while (!s.isEmpty()) {
			System.out.println(s.pop());
		}
	}

}
